<?php

class ControllerController extends AppController{

	var $uses = array('User');


	function beforeFilter() {

//		$this->checkUser();
		$this->Auth->allow('index');
		parent::beforeFilter();
	}

	function checkUser(){

		if(!$this->Session->check('User')){

			if($this->Cookie->read('User')){
				$oCookie = $this->Cookie->read('User');
				$oUser = array('User' => array());
				$oUser['User']['email'] = $oCookie['email'];
				$oUser['User']['password'] = $oCookie['token'];

				$sFilter = " email = '".$oCookie['email']."' AND ";
				$sFilter .= " password = '".$oCookie['token']."' ";
				$this->data = $this->User->find($sFilter);
				if(!empty($this->data))
					$this->Session->write('User', $this->data);
				}
		}
	}

	function index($sTopic = 'home', $sCategory = null, $sParamOne = null, $sParamTwo = null){

		$this->sTopic = $sTopic;
		$this->sCategory = $sCategory;
		$this->sParamOne = $sParamOne;
		$this->sParamTwo = $sParamTwo;
		$this->loadTop();
		$this->loadLeft();
		$this->loadMiddle();
		$this->loadRight();
		$this->loadBottom();
	}


	function loadTop(){

			/* load masthead */
		$pathMasthead = '/topics/displayMasthead/'.$this->sTopic;
		$this->set('htmlMasthead', $this->requestAction(
			$pathMasthead,
			array('return'))
		);
		/* load username */
		$path = '/users/menu/';
		$this->set('htmlUserMenu', $this->requestAction(
			$path,
			array('return'))
		);
	}

	function loadLeft(){

			/* load menue */
		$pathMenu = '/categories/display/'.$this->sTopic.'/'.$this->sCategory;
		$this->set('htmlMenu', $this->requestAction(
			$pathMenu,
			array('return'))
		);
	}

	function loadMiddle(){

		$pathContent = ($this->sCategory !== null)
			? "/$this->sTopic/display/$this->sCategory/"
			: "/topics/displayIntro/$this->sTopic"; // *)

		if($this->sTopic == 'users'){ // refactor: make display alos for users, than no distinguishing!! ... wont work because of ?  *)
			$pathContent = '/users/'.$this->sCategory.'/';
		}

		if($this->sTopic == 'registrations'){ // refactor: make display alos for users, than no distinguishing!! ... wont work because of ?  *)
			$pathContent = '/registrations/'.$this->sCategory.'/';
		}

		$pathContent .= ($this->sParamOne !== null)
			? '/'.$this->sParamOne
			: '';
		$pathContent .= ($this->sParamTwo !== null) // refactor: in index: read arguments and generic param reading here
			? '/'.$this->sParamTwo
			: '';

		$this->set('htmlContent', $this->requestAction(
			$pathContent,
			array('return'))
		);

	}

	function loadRight(){

			/* load registrations */
		if(isset($this->authUser)){
			$id = $this->authUser['id'];
			$path = '/registrations/display/'.$id;
			$this->set('htmlRegistrations', $this->requestAction(
				$path,
				array('return'))
			);
		}
		else{
			$this->set('htmlRegistrations', ''); // or put it in view? if isset()??
		}

			/* load calendar */
		$pathCalendar = '/calendar/display/';
		$this->set('htmlCalendar', $this->requestAction(
			$pathCalendar,
			array('return'))
		);
	}

	function loadBottom(){

			/* load footer */
		$pathFooter = '/topics/displayFooter/'.$this->sTopic;
		$this->set('htmlFooter', $this->requestAction(
			$pathFooter,
			array('return'))
		);
	}


}
?>

